package edu.cs.usask.ca.silver.businesslogic.ids;

import java.util.Date;
import java.util.UUID;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.commands.generic.AssociatedFile;
import edu.cs.usask.ca.silver.exceptions.SilverBusinessLogicErrorException;

/**
 * 
 * @author Yudi Xue
 * 
 */
public class FileID extends BaseId {

	public FileID(UUID value) throws SilverBusinessLogicErrorException {
		super(value);
	}

	public UUID getUUIdVal() {
		return uIdVal;
	}

	// return example: f1, f20
	public final String toString() {
		return "f" + uIdVal.toString();
	}

	@Override
	public boolean equals(BaseId id) {
		return ((id.getClass() == this.getClass()) && (((FileID) id)
				.getUUIdVal().compareTo(this.getUUIdVal()) == 0));
	}

	@Override
	public String getEntryNameFromDatabase(StorageI storage) throws Exception {
		AssociatedFile borrowedObj = ((AssociatedFile) AssociatedFile
				.getObjectPoolFactory().borrowObject());
		String name = borrowedObj.getName(this, storage);
		AssociatedFile.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	@Override
	public BaseId[] getChildrenObjectIds(StorageI storage) throws Exception {
		return null;
	}

	@Override
	public boolean isEntity() {
		return false;
	}

	@Override
	public BaseId getParentObjectId(StorageI storage) throws Exception {
		// File do not have parent
		return null;
	}

	@Override
	public boolean hasChildren() {
		// File do not have children
		return false;
	}

	public void accept(SilverIDVisitor v) {
		return;
	}

	@Override
	public String getCreatorNameFromDatabase(StorageI storage) throws Exception {
		return "Not Available";
	}

	/**
	 * File names are immutable
	 */
	@Override
	public boolean setEntryNameToDatabase(String updateName, StorageI storage) throws Exception {
		return false;
	}

	/**
	 * File don't have a type set for simulation type, so returns an empty
	 * string
	 */
	@Override
	public String getTypeFromDatabase(StorageI stoarge) throws Exception {
		return "";
	}

	/**
	 * get File description
	 * 
	 * @return get file description
	 */
	@Override
	public String getDescriptionFromDatabase(StorageI storage) throws Exception {
		AssociatedFile borrowedObj = ((AssociatedFile) AssociatedFile
				.getObjectPoolFactory().borrowObject());
		String desc = borrowedObj.getDescription(this, storage);
		AssociatedFile.getObjectPoolFactory().returnObject(borrowedObj);
		return desc;
	}

	@Override
	public boolean setDescriptionToDatabase(String desc, StorageI storage) throws Exception {
		AssociatedFile borrowedObj = ((AssociatedFile) AssociatedFile
				.getObjectPoolFactory().borrowObject());
		boolean result = borrowedObj.setDescription(this, desc, storage);
		AssociatedFile.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public Date getCreationTime(StorageI storage) throws Exception {
		return null;
	}

	@Override
	public boolean deleteFromDatabase(StorageI storage) throws Exception {
		AssociatedFile borrowedObj = ((AssociatedFile) AssociatedFile
				.getObjectPoolFactory().borrowObject());
		boolean result = borrowedObj.deleteFile(this, storage);
		AssociatedFile.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

}
